home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 5 / Gold Medal Software - Volume 5 (Gold Medal) (1995).iso / database / cff51b.arj / TREEFILE.C < prev    next >
C/C++ Source or Header  |  1993-10-22  |  9KB  |  355 lines

  1. /* TREEFILE.C */
  2. /* Copyright 1990, 1991, 1992, 1993 Norman D. Culver Ft. Lauderdale, FL */
  3. /*                    All Rights Reserved                                   */
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "../cff.h"
  8.  
  9. #define CLOCKS_PER_SECOND 1000000L
  10.  
  11. /* TEST CHOICES */
  12. #define PFLAGS 0
  13. #define MEMFILE 1
  14. #define EXTDMEM 1
  15. #define CFFFILE 1
  16.  
  17. extern int errno;
  18. extern long clock();
  19. extern int rand();
  20.  
  21.  
  22. static void file_test(void *hd, void *hd1);
  23. static void big_test(void *hd, void *hd1);
  24.  
  25. void
  26. main()
  27. {
  28. void *hxxx;
  29. void *huniq;
  30. void *hcfile;
  31. void *hsxxx;
  32. void *hsxxx1;
  33. void *hcxxx;
  34.  
  35.     cfport_settestflags(1);
  36.     cfinit("treefile",512,NULL);
  37.     cfprintf("B+ TREE DIRECTORY -- test of file property 4bytes per i/o\n");
  38.  
  39. #if MEMFILE
  40.     cfprintf("MEMORY FILE\n");
  41.     hxxx = cfopen("MEMORY/XXX",F_RDWR|F_CREAT,NULL);
  42.     if(!hxxx) {
  43.         cfprintf("ERROR: create XXX err=%d\n", errno);
  44.         exit(0);
  45.     }
  46.     huniq = cfopen("MEMORY/XXX", F_RDWR|F_CREAT|F_TEMP|F_UNIQ|F_SORTED, NULL);
  47.     if(!huniq) {
  48.         cfprintf("ERROR: create UNIQUE TEMP FILE err=%d\n", errno);
  49.         exit(0);
  50.     }
  51. #if PFLAGS
  52.     cfpflags("hxxx", hxxx);
  53.     cfpflags("huniq", huniq);
  54. #endif
  55.     file_test(huniq, NULL);
  56.     big_test(huniq, NULL);
  57. cfprintf("NOW CLOSING\n");
  58.     cfclose(huniq);
  59. #endif /* MEMFILE */
  60.  
  61. #if EXTDMEM
  62.     cfprintf("EXTDMEM FILE\n");
  63.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR|F_CREAT|F_SORTED, NULL);
  64.     if(!hsxxx) {
  65.         cfprintf("ERROR: create EXTDMEM/XXX err=%d\n", errno);
  66.         exit(0);
  67.     }
  68.     {/* Special case file, with preallocation of local bitmap */
  69.     OPNINFO info;
  70.         info.initial_entries = 0;
  71.         info.bitmap_prealloc = 240000;
  72.         info.data_prealloc = 0;
  73.         hsxxx1 = cfopen(    "EXTDMEM/XXX1",
  74.                             F_RDWR|F_CREAT|F_BITMAP|F_TEMP|F_SORTED,
  75.                             &info);
  76.         if(!hsxxx1) {
  77.             cfprintf("ERROR: create EXTDMEM/XXX1 err=%d\n", errno);
  78.             exit(0);
  79.         }
  80.     }
  81. #if PFLAGS
  82.     cfpflags("hsxxx", hsxxx);
  83.     cfpflags("hsxxx1", hsxxx1);
  84. #endif
  85.     file_test(hsxxx, hsxxx1);
  86.     big_test(hsxxx1, NULL);
  87. cfprintf("NOW CLOSING\n");
  88.     cfclose(hsxxx1);
  89.     cfclose(hsxxx);
  90.  
  91.     cfprintf("REOPEN EXTDMEM FILE\n");    
  92.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR, NULL);
  93.     if(!hsxxx) {
  94.         cfprintf("ERROR: reopen EXTDMEM:/XXX err=%d\n", errno);
  95.         exit(0);
  96.     }
  97.     file_test(hsxxx,NULL);
  98.     big_test(hsxxx,NULL);
  99.     cfunlink(hsxxx, NULL);
  100. #endif /* EXTDMEM */
  101.  
  102. #if CFFFILE
  103.     cfprintf("CFF DISK FILE\n");
  104.     hcfile = cfopen("testfile.cff", F_RDWR|F_CREAT, NULL);
  105.     if(!hcfile) {    
  106.         cfprintf("ERROR: create testfile.cff err=%d\n", errno);
  107.         exit(0);
  108.     }
  109.     hcxxx = cfopen("testfile.cff/XXX",
  110.             F_RDWR|F_CREAT|F_SORTED, NULL);
  111.     if(!hcxxx) {    
  112.         cfprintf("ERROR: create testfile.cff/XXX err=%d\n", errno);
  113.         exit(0);
  114.     }
  115. #if 0
  116.     cfpflags("hcfile", hcfile);
  117.     cfpflags("hcxxx", hcxxx);
  118. #endif
  119.     file_test(hcxxx, NULL);
  120. cfprintf("NOW CLOSING\n");
  121.     cfclose(hcxxx);
  122.     cfclose(hcfile);
  123.  
  124.     cfprintf("REOPEN CFF DISK FILE\n");    
  125.     hcfile = cfopen("testfile.cff", F_RDWR, NULL);
  126.     if(!hcfile) {    
  127.         cfprintf("ERROR: reopen testfile.cff err=%d\n", errno);
  128.         exit(0);
  129.     }
  130.     hcxxx = cfopen("testfile.cff/XXX", F_RDWR, NULL);
  131.     if(!hcxxx) {    
  132.         cfprintf("ERROR: reopen testfile.cff/XXX err=%d\n", errno);
  133.         exit(0);
  134.     }
  135.     file_test(hcxxx,NULL);
  136.     big_test(hcxxx,NULL);
  137. cfprintf("NOW CLOSING\n");
  138.     cfclose(hcxxx);
  139.     cfclose(hcfile);
  140.     cfunlink("testfile.cff");
  141. #endif /* CFF FILE */
  142.  
  143.     cfexit();
  144. }
  145.  
  146. static void
  147. file_test(void *hd, void *hd1)
  148. {
  149. long i, start, end, diff, err;
  150. long amount;
  151. long obtype = cfobtype(hd);
  152. int nfile = (hd1)?2:1;
  153.  
  154.         if(obtype & OB_MEM)
  155.             amount = 250000;
  156.         else if(obtype & OB_CFILE)
  157.             amount = 100000;
  158.         else if(obtype & OB_XFILE)
  159.             amount = 10000;
  160.         else if(obtype & OB_SMEM)
  161.             amount = 150000;
  162.         else {
  163.             cfprintf("UNKNOWN OBTYPE: %x\n", obtype);
  164.             return;
  165.         }
  166.  
  167.         /* WRITE DATA */
  168.         start = clock();
  169.         if(cfisnew(hd)) {
  170.             cfseek(hd, 0, S_SET);
  171.             if(hd1)
  172.                 cfseek(hd1, 0, S_SET);
  173.             for(i = 0; i < amount; ++i) {
  174.                 if((err = cfwrite(hd, &i, 4)) != 4) {
  175.                     cfprintf("  File write failed at i=%d err=%d\n", i, err);
  176.                     break;
  177.                 }
  178.                 /* SECOND FILE, forces multiple noncontiguous chunks into
  179.                     both file maps */
  180.                 if(hd1) if((err = cfwrite(hd1, &i, 4)) != 4) {
  181.                     cfprintf("  File write1 failed at i=%d err=%d\n", i, err);
  182.                     break;
  183.                 }
  184.             }
  185.             end = clock();
  186.             diff = end - start;
  187.             diff /= CLOCKS_PER_SECOND/10;
  188.             if(diff == 0) diff = 1;
  189.             cfprintf("  FILEWRITES PER SEC = %ld\n", (nfile*i*10) / diff);
  190.         }
  191. cfprintf("  Bytesalloc=%lu Bytesused=%lu\n  File1 size=%lu File1 alloc=%lu\n",
  192. cfbytesalloc(hd), cfbytesused(hd), cffilesize(hd), cffilealloc(hd));
  193. if(nfile == 2)
  194. cfprintf("  File2 size=%lu File2 alloc=%lu\n", cffilesize(hd1), cffilealloc(hd1));
  195.  
  196.         /* FORWARD */
  197.         start = clock();
  198.         cfseek(hd, 0, S_SET);
  199.         for(i = 0; i < amount; ++i) {
  200.         long val;
  201.             if((err = cfread(hd, &val, 4)) != 4) {
  202.                 cfprintf("  File read failed at i=%ld err=%ld val=%ld\n", i, err,val);
  203.                 break;
  204.             }
  205.             if(val != i) {
  206.                 cfprintf("  File read bad value=%d should be %d\n", val, i);
  207.                 break;
  208.             }
  209.         }
  210.         end = clock();
  211.         diff = end - start;
  212.         diff /= CLOCKS_PER_SECOND/10;
  213.         if(diff == 0) diff = 1;
  214.         cfprintf("  FILEREADS PER SEC = %ld\n", (i*10) / diff);
  215.  
  216.         /* BACKWARD */
  217.         start = clock();
  218.         for(i = amount-1; i >= 0; --i) {
  219.         long val;
  220.             cfseek(hd, i*4, S_SET);
  221.             if((err = cfread(hd, &val, 4)) != 4) {
  222.                 cfprintf("  File backward read failed at i=%d err=%d\n", i, err);
  223.                 break;
  224.             }
  225.             if(val != i) {
  226.                 cfprintf("  File backward read bad value=%d should be %d\n", val, i);
  227.                 break;
  228.             }
  229.         }
  230.         end = clock();
  231.         diff = end - start;
  232.         diff /= CLOCKS_PER_SECOND/10;
  233.         if(diff == 0) diff = 1;
  234.         cfprintf("  BACKWARD READS PER SEC = %ld\n", ((amount -i)*10) / diff);
  235.  
  236.         /* RANDOM */
  237.         start = clock();
  238.         for(i = 0; i < amount/10; ++i) {
  239.         long val, j;
  240.  
  241.             j = rand() % amount;
  242.             cfseek(hd, j*4, S_SET);
  243.             if((err = cfread(hd, &val, 4)) != 4) {
  244.                 cfprintf("  File random read failed at j=%d err=%d\n", j, err);
  245.                 break;
  246.             }
  247.             if(val != j) {
  248.                 cfprintf("  File random read bad value=%d should be %d\n", val, j);
  249.                 break;
  250.             }
  251.         }
  252.         end = clock();
  253.         diff = end - start;
  254.         diff /= CLOCKS_PER_SECOND/10;
  255.         if(diff == 0) diff = 1;
  256.         cfprintf("  RANDOM READS PER SEC = %ld\n", (i*10) / diff);
  257.  
  258.  
  259.     /* CLIP THE FILESIZE SUBSTANTIALLY */
  260.     if(!cfisnew(hd))
  261.     {
  262.         amount /= 2;
  263.  
  264.         /* TRUNCATE AND REREAD FILE */
  265.         cfprintf("   TRUNCATE AND REREAD FILE\n");
  266.  
  267.         cftruncate(hd, amount * 4);
  268.         start = clock();
  269.         cfseek(hd, 0, S_SET);
  270.         for(i = 0; i < amount; ++i) {
  271.         long val;
  272.             if((err = cfread(hd, &val, 4)) != 4) {
  273.                 cfprintf("  File read failed at i=%ld err=%ld val=%ld\n", i, err,val);
  274.                 break;
  275.             }
  276.             if(val != i) {
  277.                 cfprintf("  File read bad value=%d should be %d\n", val, i);
  278.                 break;
  279.             }
  280.         }
  281.         end = clock();
  282.         diff = end - start;
  283.         diff /= CLOCKS_PER_SECOND/10;
  284.         if(diff == 0) diff = 1;
  285.         cfprintf("  FILEREREADS PER SEC = %ld\n", (i*10) / diff);
  286. cfprintf("  Bytesalloc=%lu Bytesused=%lu\n  File1 size=%lu File1 alloc=%lu\n",
  287. cfbytesalloc(hd), cfbytesused(hd), cffilesize(hd), cffilealloc(hd));
  288.     }
  289. }
  290. static void
  291. big_test(void *hd, void *hd1)
  292. {
  293. #define BUFSIZE 15000
  294. long buf[BUFSIZE];
  295. int i,j;
  296. long err;
  297.     cfprintf("  TEST LARGE BUFFER READ/WRITE\n");
  298.  
  299.  
  300.     cfseek(hd, 0, S_SET);
  301.     if(hd1)
  302.         cfseek(hd1, 0, S_SET);
  303.  
  304.     cfprintf("  WRITE 10 BUFFERS of %lu bytes each\n", sizeof(buf));
  305.     for(i = 0; i < 10; ++i)
  306.     {
  307.         for(j = 0; j < BUFSIZE-1; ++j)
  308.             buf[j] = (j<<8)+i;
  309.         if((err = cfwrite(hd, buf, sizeof(buf))) != sizeof(buf)) {
  310.             cfprintf("BIG write error hd i=%d err=%d\n", i, err);
  311.         }
  312.         if(hd1)
  313.             if((err = cfwrite(hd1, buf, sizeof(buf))) != sizeof(buf)) {
  314.                 cfprintf("BIG write error hd1 i=%d err=%d\n", i, err);
  315.             }
  316.     }
  317.     cfprintf("  WRITE DONE\n");
  318.  
  319.     cfseek(hd, 0, S_SET);
  320.     if(hd1)
  321.         cfseek(hd1, 0, S_SET);
  322.     cfprintf("  READ AND CHECK BUFFERS\n");
  323.     for(i = 0; i < 10; ++i)
  324.     {
  325.         memset(buf, 0, sizeof(buf));
  326.         if((err = cfread(hd, buf, sizeof(buf))) != sizeof(buf)) {
  327.             cfprintf("BIG READ ERROR hd i=%d err=%d\n", i, err);
  328.             continue;
  329.         }
  330.         for(j = 0; j < BUFSIZE-1; ++j) {
  331.             if(buf[j] != (j<<8)+i) {
  332.                 cfprintf("BAD DATA hd %x should be %x at i=%d j=%d\n",
  333.                     buf[j], (j<<8)+i, i, j);    
  334.                 break;
  335.             }
  336.         }
  337.         if(hd1) {
  338.             memset(buf, 0, sizeof(buf));
  339.             if((err = cfread(hd1, buf, sizeof(buf))) != sizeof(buf)) {
  340.                 cfprintf("BIG READ ERROR hd1 i=%d err=%d\n", i, err);
  341.                 continue;
  342.             }
  343.             for(j = 0; j < BUFSIZE-1; ++j) {
  344.                 if(buf[j] != (j<<8)+i) {
  345.                     cfprintf("BAD DATA hd %x should be %x at i=%d j=%d\n",
  346.                         buf[j], (j<<8)+i, i, j);    
  347.                     break;
  348.                 }
  349.             }
  350.         }
  351.     }
  352.     cfprintf("  READ AND CHECK DONE\n");
  353. }
  354.  
  355.